﻿using Microsoft.SemanticKernel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntermediateDemo;

internal class InputPrompt
{
    public static async Task SafeInputVariableAsync(Kernel kernel)
    {
        var kernelArguments = new KernelArguments()
        {
            ["input"] = "什么是北京?",
        };
        var chatPrompt = """
                     <message role="user">{{$input}}</message>
                     """;
        Console.WriteLine(await RenderPromptAsync(kernel, new PromptTemplateConfig(chatPrompt), kernelArguments));
        Console.WriteLine(await kernel.InvokePromptAsync(chatPrompt, kernelArguments));
    }

    static readonly IPromptTemplateFactory _promptTemplateFactory = new KernelPromptTemplateFactory();

    static Task<string> RenderPromptAsync(Kernel kernel, PromptTemplateConfig promptConfig, KernelArguments? arguments = null,
      IPromptTemplateFactory? promptTemplateFactory = null)
    {
        promptTemplateFactory ??= _promptTemplateFactory;
        var promptTemplate = promptTemplateFactory.Create(promptConfig);
        return promptTemplate.RenderAsync(kernel, arguments);
    }
}
